#include "engrpmpanel.h"
#include "qpainter.h"

EngRPMPanel::EngRPMPanel(QWidget* parent):
    QWidget(parent)
{

}

void EngRPMPanel::paintEvent(QPaintEvent *)
{
    int width = this->width();
    int height = this->height();
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.translate(width/2, height/2);
    int side = qMin(width, height);
    painter.scale(side / 200.0, side / 200.0);

    drawRing(&painter);
    drawPointer(&painter);
    drawLabel(&painter);
    drawNum(&painter);

}

void EngRPMPanel::drawRing(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    //黄段
    painter->setBrush(QColor(244,177,131,255));
    painter->drawPie(-100,-100,200,200,130*16,80*16);
    //蓝段
    painter->setBrush(QColor(157,195,230,255));
    painter->drawPie(-100,-100,200,200,10*16,120*16);
    //紫段
    painter->setBrush(QColor(145,72,200,255));
    painter->drawPie(-100,-100,200,200,330*16,40*16);
    //中心遮挡圆
    painter->setBrush(QColor(34,42,53,255));
    painter->drawPie(-90,-90,180,180,330*16,360*16);
    //尺度
    QPen pen;
    pen.setColor(QColor(255,255,255,255));
    pen.setWidthF(1.0);
    painter->setPen(pen);
    painter->rotate(-210);
    painter->drawLine(80,0,90,0);
    painter->drawText(60,-10,20,20,Qt::AlignCenter,QString("%1").arg(0),Q_NULLPTR);
    for(int i=0;i<6;i++)
    {
        painter->rotate(40);
        painter->drawLine(80,0,90,0);
        painter->drawText(60,-10,20,20,Qt::AlignCenter,QString("%1").arg(i*5+5),Q_NULLPTR);
    }
    painter->rotate(-232);
    painter->drawLine(85,0,90,0);
    for(int i=0;i<28;i++)
    {
        painter->rotate(8);
        painter->drawLine(85,0,90,0);
    }
    painter->restore();
}

void EngRPMPanel::drawPointer(QPainter *painter)
{
    painter->save();
    float RPM = this->RPM/100.0;
    float angle = 8.0*RPM-30;
    painter->rotate(angle);
    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor(157,195,230,255));
    static const QPointF points[4] = {
          QPointF(-75.0, 0.0),
          QPointF(0.0, -5.0),
          QPointF(10.0, 0.0),
          QPointF(0.0, 5.0)
      };
    painter->drawPolygon(points,4);
    QPen pen;
    pen.setColor(QColor(0,0,0,255));
    pen.setWidthF(3.0);
    painter->setPen(pen);
    painter->drawPoint(0,0);
    painter->restore();
}

void EngRPMPanel::drawLabel(QPainter *painter)
{
    int RPM = this->RPM;
    painter->save();
    QPen pen;
    pen.setColor(QColor(255,255,255,255));
    pen.setWidthF(1.0);
    painter->setPen(pen);
    QFont font = painter->font();
    font.setPixelSize(10);
    font.setFamily("微软雅黑");
    painter->setFont(font);
    painter->drawText(-15,-53,30,20,Qt::AlignCenter,QString("RPM"),Q_NULLPTR);
    painter->drawText(-15,-40,30,20,Qt::AlignCenter,QString("×100"),Q_NULLPTR);
    painter->restore();
}

void EngRPMPanel::drawNum(QPainter *painter)
{
    int RPM = this->RPM;
    painter->save();

    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor(0,0,0,255));
    static const QPointF points[4] = {
          QPointF(-35.0, 30.0),
          QPointF(35.0, 30.0),
          QPointF(35.0, 50.0),
          QPointF(-35.0,50.0)
      };
    painter->drawPolygon(points,4);

    QPen pen;
    pen.setColor(QColor(255,255,255,255));
    pen.setWidthF(1.0);
    painter->setPen(pen);
    QFont font = painter->font();
    font.setPixelSize(20);
    font.setFamily("微软雅黑");
    painter->setFont(font);
    painter->drawText(-35,30,70,20,Qt::AlignCenter,QString("%1").arg(int(RPM)),Q_NULLPTR);
    painter->restore();
}

void EngRPMPanel::setRPMValue(int RPM)
{
    this->RPM = RPM;
}
